window.onload = function() {
    var box = document.querySelector('div');
    box.onmousedown = function(e) {
        var mousedownleft = e.pageX,
            mousedowntop = e.pageY;
        var boxleft = box.offsetLeft,
            boxtop = box.offsetTop;
        document.onmousemove = function(e) {
            box.style.left = e.pageX - (mousedownleft - boxleft) + 'px';
            box.style.top = e.pageY - (mousedowntop - boxtop) + 'px';
        }
    }
    box.onmouseup = function() {
        document.onmousemove = null;
    }
}